$(document).ready(function(){
//	获取餐桌信息
	$('.desk').click(function(){
	$.ajax({
		url:'dt_getDataList.action',
		type:'POST',
		dataType: 'json',
		success: function(data){
			if (data.status == "200") {
				$(".desk_table  tr:not(:first)").empty("");
				for (var i = 0; i <data.data.length; i++) {
					var value = data.data[i];
					var tr;
					if (i % 2 != 0) {
						tr = $("<tr class='info'></tr>");
					} else {
						tr = $("<tr></tr>");
					}
					var row=tr.append("<td>"+value.id+"</td><td>"+value.tname+"</td><td>"+value.name+"</td><td>"+value.locationEnumName+"</td><td>"+value.creatUname+"</td>"+
							"<td><a href='#desk_update' class='btn btn-info update_desk' data-toggle='modal'>修改</a><a href='#' class='btn btn-warning delete_desk'style='margin-left:10px;'>删除</a></td>").appendTo('.desk_table');
				}
		}
			else{
				window.location.href = "error.html";
			}
	},
	});
	});
//	封装餐桌信息
	function getDesk(){
		$.ajax({
			url:'dt_getDataList.action',
			type:'POST',
			dataType: 'json',
			success: function(data){
				if (data.status == "200") {
					$(".desk_table  tr:not(:first)").empty("");
					for (var i = 0; i <data.data.length; i++) {
						var value = data.data[i];
						var tr;
						if (i % 2 != 0) {
							tr = $("<tr class='info'></tr>");
						} else {
							tr = $("<tr></tr>");
						}
						var row=tr.append("<td>"+value.id+"</td><td>"+value.tname+"</td><td>"+value.name+"</td><td>"+value.locationEnumName+"</td><td>"+value.creatUname+"</td>"+
						"<td><a href='#desk_update' class='btn btn-info update_desk' data-toggle='modal'>修改</a><a href='#' class='btn btn-warning delete_desk'style='margin-left:10px;'>删除</a></td>").appendTo('.desk_table');
					}
			}
				else{
					window.location.href = "error.html";
				}
		},
		});
	};
//	获取要修改的餐桌信息
	
	$('body').on('click','.update_desk',function(){
		 var id=$(this).parent().parent().children().eq(0).text();
		 var value;
		 $.ajax({
			 url:'dt_sreachDataById.action',
				type:'POST',
				dataType: 'json',
				data:{'id':id},
				success: function(data){
					value=data.data;
					$('#update_desk_tname').val(data.data.tname);
					$('#update_desk_name').val(data.data.name);
					$('#update_desk_locationEnumName').val(data.data.locationEnumName);
					$('#update_desk_creatUname').val(data.data.creatUname);
				}
		 }).done(function(){//修改餐桌信息
			 $('#btn_desk_update').unbind('click').bind('click',function(){
			 //$('body').on('click','#btn_desk_update',function(){
				 $.ajax({
					 url:'dt_changeDataInfo.action',
						type:'POST',
						dataType: 'json',
						data:{'id':id,
							'tname':$('#update_desk_tname').val(),
							'name':$('#update_desk_name').val(),
							'locationEnumName':$('#update_desk_locationEnumName').val(),
							'creatUname':$('#update_desk_creatUname').val(),
							'enumCode':value.enumCode,
							'tid':value.tid,
							'locationEnumCode':value.locationEnumCode,
							},
						success: function(data){
							if (data.status == "200"){
								getDesk();
							}
							else{
								window.location.href = "error.html";
							}
						}
				 });
			 });
		 });
	 });
//餐桌信息删除
	$('body').on('click','.delete_desk',function(){
		 var id=$(this).parent().parent().children().eq(0).text();
		 $.ajax({
			 url:'dt_removeData.action',
				type:'POST',
				dataType: 'json',
				data:{'id':id},
				success: function(data){
					if (data.status == "200"){
						getDesk();
					}
					else{
						window.location.href = "error.html";
					}
				}
		 });
	 });
//	添加餐桌
	var nameTemp=null;
	$('.add-desk').click(function(){
		if($('#cancle').length>0){
		}
		else{
		var row=$("<tr class='warning' id='cancle'></tr>");
		row.append("<td></td><td><input type='text'></td><td><input type='text'></td><td><input type='text'></td>" +
				"<td><td><button class='btn btn-success complete-add-desk' id='complete-add-desk'>完成</button><button class='btn btn-deleteinputs cancle-add-desk' id='cancle-add-desk' style='margin-left:10px;background-color:red;color:white;'>取消</button></td>").appendTo(".desk_table");
		$('body').on('click','#complete-add-desk',function(){
			var tname=$(this).parent().parent().children().eq(1).children().val();
			var name=$(this).parent().parent().children().eq(2).children().val();
			var locationEnumName=$(this).parent().parent().children().eq(3).children().val();
			$('#cancle').remove();
			
			if(tname==""||name==""||locationEnumName==""){
				alert("来自某宝客服的提醒：亲，这些都是必填哦！");
			}
			else{
				if(nameTemp!=name){
					nameTemp=name;
					$.ajax({
						url:'dt_addData.action',
						type:'POST',
						dataType:'json',
						data:{
							'name':name,
							'tname':tname,
							'locationEnumName':locationEnumName,
							'creatUname':'慌得要死'//后期修改
						},
						success:function(data){
							if (data.status == "200"){
								getDesk();
							}
							else{
								window.location.href = "error.html";
							}
						}
						});
				}else{
					return;
				}
			
		}
		});
		$('body').on('click','.cancle-add-desk',function(){//点击加号后取消
			$('#cancle').remove();
			$('#cancle>td').remove();
			$('#cancle>td>input').remove();
		});
	}
	});
});